package com.yeren.socket.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 * 客户端应用程序
 */
public class Client {
	private Socket socket;

	public Client() throws UnknownHostException, IOException {
		try {
			/**
			 * 创建Socket对象时，就会尝试根据给定的地址与端口连接服务端 所以，若对象创建成功，说明与服务端连接正常
			 */
			System.out.println("正在连接服务端...");
			socket = new Socket("localhost", 8888);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		}
	}
	/**
	 * 客户端启动方法
	 */
	public void start() {
		System.out.println("客户端启动start!!!");
		try {
			// 创建并启动线程，来接受服务端的消息
			Runnable runn = new GetServerInfoHandler();
			Thread t = new Thread(runn);
			t.start();
			/**
			 * 可以通过Socket的getOutputStream() 方法获取一条输出流，用于将信息发送至服务端
			 */
			OutputStream out = socket.getOutputStream();
			
			/**
			 * 使用字符流来根据指定的编码集将字符串转换为字节后，再通过out发送给服务端
			 */
			OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");
			
			/**
			 * 将字符流包装成缓冲字符流，就可以按行为单位写出字符串了
			 */
			PrintWriter pw = new PrintWriter(osw, true);
			
			/**
			 * 创建一个Scanner,用于接收用户输入的字符串
			 */
			Scanner sc = new Scanner(System.in);
			System.out.println("欢迎来到刘彪的聊天室");
			while (true) {
				System.out.println("请输入昵称：");
				String nickname = sc.nextLine();
				if (nickname.trim().length() > 0) {
					pw.println(nickname);
					break;
				}
				System.out.println("昵称不能为空！");
			}

			while (true) {
				String str = sc.nextLine();
				pw.println(str);
			}
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	public static void main(String[] args) {
		try {
			Client client = new Client();
			client.start();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("客户端初始化失败");
		}
	}

	class GetServerInfoHandler implements Runnable {
		public void run() {
			try {
				/**
				 * 通过Socket获取输入流
				 */
				InputStream in = socket.getInputStream();
				
				/**
				 * 将输入流转换为字符流输入，指定编码集
				 */
				InputStreamReader isr = new InputStreamReader(in, "utf-8");
				
				/**
				 * 将字符输入流转换为缓冲流
				 */
				BufferedReader br = new BufferedReader(isr);
				String message = null;
				//循环读取服务端发送的每一个字符串
				while ((message = br.readLine()) != null) {
					//将服务端发送的字符串输出到控制台
					System.out.println(message);
				}
			} catch (Exception e) {

			}
		}
	}

}
